package jp.co.hcs.attendancemanagement.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;

/**
 * The Class StringUtil.
 */
public class StringUtil {

  /** The Constant ZERO. */
  public static final String ZERO = "0";

  /** The Constant EMPTY. */
  public static final String EMPTY = "";

  /** The Constant SPACE_VALUE. */
  public static final String SPACE_VALUE = "";

  /** The Constant EDITING_VALUE. */
  public static final String EDITING_VALUE = "編集中";

  /** The Constant BACK_VALUE. */
  public static final String BACK_VALUE = "差戻し";

  /** The Constant CONFIRMED_VALUE. */
  public static final String CONFIRMED_VALUE = "確定済";

  /** The SUNDAY. */
  public static String SUNDAY = "Sunday";

  /** The SATURDAY. */
  public static String SATURDAY = "Saturday";

  /** The day name of week map. */
  public static HashMap<String, String> dayNameOfWeekMap;

  static {
    dayNameOfWeekMap = new HashMap<String, String>();
    dayNameOfWeekMap.put("Monday", "月");
    dayNameOfWeekMap.put("Tuesday", "火");
    dayNameOfWeekMap.put("Wednesday", "水");
    dayNameOfWeekMap.put("Thursday", "木");
    dayNameOfWeekMap.put("Friday", "金");
    dayNameOfWeekMap.put("Saturday", "土");
    dayNameOfWeekMap.put("Sunday", "日");
  }

  /**
   * Converts string contains fullwidth digit to half width digit.
   * 
   * @param str string before transformation (null is impossible)
   * @return string after transformation
   */
  public static String fullWidthNumberToHalfWidthNumber(String str) {
    if (str == null) {
      throw new IllegalArgumentException();
    }
    StringBuffer sb = new StringBuffer(str);
    for (int i = 0; i < str.length(); i++) {
      char c = str.charAt(i);
      if ('０' <= c && c <= '９') {
        sb.setCharAt(i, (char) (c - '０' + '0'));
      }
    }
    return sb.toString();
  }

  /**
   * Parses the zero string.
   * 
   * @param num the number
   * @return the string
   */
  public static String parseZeroString(int num) {
    if (num < 10) {
      return ZERO + num;
    }
    return String.valueOf(num);
  }

  // convert String to display time in total time field
  /**
   * Convert string total timeto display format.
   * 
   * @param totalTime the total time
   * @return the string
   */
  public static String convertStringTotalTimetoDisplayFormat(String totalTime) {
    if (isEmpty(totalTime)) {
      return totalTime;
    }
    int totalTimeInt = Integer.parseInt(totalTime);
    int hours = totalTimeInt / 10000;
    int minutes = totalTimeInt % 10000;
    if (minutes > 6000) {
      minutes = (minutes - 6000) / 100;
      hours = hours + 1;
    } else {
      minutes = minutes / 100;
    }
    return parseZeroString(hours) + ":" + parseZeroString(minutes);
  }

  /**
   * Gets the days in month.
   * 
   * @param year the year
   * @param month the month
   * @return the days in month
   */
  public static int getDaysInMonth(int year, int month) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(year, month - 1, 1);
    int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    return days;
  }

  /**
   * Gets the system year.
   * 
   * @return the system year
   */
  public static String getSystemYear() {
    Calendar cal = new GregorianCalendar();
    return cal.get(Calendar.YEAR) + "";
  }

  /**
   * Gets the system month.
   * 
   * @return the system month
   */
  public static String getSystemMonth() {
    Calendar cal = new GregorianCalendar();
    return parseZeroString(1 + cal.get(Calendar.MONTH));
  }

  /**
   * Gets the system date.
   * 
   * @return the system date
   */
  public static String getSystemDate() {
    Calendar cal = new GregorianCalendar();
    return parseZeroString(cal.get(Calendar.DAY_OF_MONTH));
  }

  /**
   * Gets the system day of week.
   * 
   * @return the system day of week
   */
  public static String getSystemDayOfWeek() {
    Calendar cal = new GregorianCalendar();
    return cal.get(Calendar.DAY_OF_WEEK) + "";
  }

  /**
   * Gets the day name.
   * 
   * @param year the year
   * @param month the month
   * @param dayNumber the day number
   * @return the day name
   */
  public static String getDayName(int year, int month, int dayNumber) {
    Date date1 = (new GregorianCalendar(year, month - 1, dayNumber)).getTime();

    DateFormat f = new SimpleDateFormat("EEEE");
    try {
      return f.format(date1);
    } catch (Exception e) {
      e.printStackTrace();
      return "";
    }
  }

  /**
   * Checks if is empty.
   * 
   * @param str the str
   * @return true, if is empty
   */
  public static boolean isEmpty(String str) {
    if (str == null) {
      return true;
    }

    return "".equals(str);
  }

  /**
   * Fill zero before.
   * 
   * @param target the target
   * @param length the length
   * @return the string
   */
  public static String fillZeroBefore(String target, int length) {
    StringBuilder sb = new StringBuilder();
    for (int i = target.length(); i < length; i++) {
      sb.append(ZERO);
    }
    sb.append(target);
    return sb.toString();
  }
  
  public static String caculateTotalTime(int inputTotalMinutes){
	  int totalHours = inputTotalMinutes / 60;
	  int totalMinutes = inputTotalMinutes % 60;
	  String totalHoursStr = totalHours < 10 ? ("0"+totalHours) : ""+totalHours;
	  String totalMinutesStr = totalMinutes < 10 ? ("0"+totalMinutes) : ""+totalMinutes;
	  return totalHoursStr+":"+totalMinutesStr;
  }
  
  public static int convertTimesToMinutes(String timeFormatsValue){
	  int minutes = 0;
	  if(timeFormatsValue != null && timeFormatsValue.length() == 5){
		  int hours = Integer.parseInt(timeFormatsValue.substring(0, 2));
		  minutes = hours*60 + Integer.parseInt(timeFormatsValue.substring(3, 5));
	  }
	  return minutes; 
  }

}
